package cn.iocoder.yudao.module.biz.controller.admin.risk.taskWork;

import cn.iocoder.yudao.module.biz.controller.admin.risk.basicManage.vo.BizBasicManageExcelVO;
import cn.iocoder.yudao.module.biz.controller.admin.risk.basicManage.vo.BizBasicManageImportRespVO;
import cn.iocoder.yudao.module.biz.controller.admin.risk.basicManage.vo.BizBasicManageRespVO;
import cn.iocoder.yudao.module.biz.controller.admin.risk.taskWork.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.risk.taskWork.BizTaskWorkDO;
import cn.iocoder.yudao.module.biz.service.risk.taskWork.BizTaskWorkService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.io.BufferedOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.*;
import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;
import cn.iocoder.yudao.module.biz.convert.risk.taskWork.BizTaskWorkConvert;
import org.springframework.web.multipart.MultipartFile;

@Api(tags = "管理后台 - 作业活动")
@RestController
@RequestMapping("/biz/task-work")
@Validated
public class BizTaskWorkController {

    @Resource
    private BizTaskWorkService taskWorkService;

    @PostMapping("/create")
    @ApiOperation("创建作业活动")
    @PreAuthorize("@ss.hasPermission('biz:task-work:create')")
    public CommonResult<String> createTaskWork(@Valid @RequestBody BizTaskWorkCreateReqVO createReqVO) {
        return success(taskWorkService.createTaskWork(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新作业活动")
    @PreAuthorize("@ss.hasPermission('biz:task-work:update')")
    public CommonResult<Boolean> updateTaskWork(@Valid @RequestBody BizTaskWorkUpdateReqVO updateReqVO) {
        taskWorkService.updateTaskWork(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除作业活动")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:task-work:delete')")
    public CommonResult<Boolean> deleteTaskWork(@RequestParam("id") String id) {
        taskWorkService.deleteTaskWork(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得作业活动")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:task-work:query')")
    public CommonResult<BizTaskWorkRespVO> getTaskWork(@RequestParam("id") String id) {
        BizTaskWorkDO taskWork = taskWorkService.getTaskWork(id);
        return success(BizTaskWorkConvert.INSTANCE.convert(taskWork));
    }

    @GetMapping("/list")
    @ApiOperation("获得作业活动列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('biz:task-work:query')")
    public CommonResult<List<BizTaskWorkRespVO>> getTaskWorkList(@RequestParam("ids") Collection<String> ids) {
        List<BizTaskWorkDO> list = taskWorkService.getTaskWorkList(ids);
        return success(BizTaskWorkConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得作业活动分页")
    @PreAuthorize("@ss.hasPermission('biz:task-work:query')")
    public PageResult<BizTaskWorkRespVO> getTaskWorkPage(@Valid BizTaskWorkPageReqVO pageVO) {
        PageResult<BizTaskWorkRespVO> result=new PageResult<>();
        List<BizTaskWorkRespVO> list = taskWorkService.getTaskPage(pageVO);
        Integer count= taskWorkService.getTaskCount(pageVO);
        result.setList(list);
        result.setTotal((long) count);
        return result;
    }

    @PostMapping(value = "/downTaskTemplate")
    @ApiOperation("下载作业活动的模板")
    public HttpServletResponse downRiskTemplate(HttpServletResponse response,HttpServletRequest request) {
        try {
            String filename = "作业活动导入模板.xlsx";
            InputStream fis = this.getClass().getClassLoader().getResourceAsStream("template/作业活动导入模板.xls");
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            // 清空response
            response.reset();
            // 设置response的Header
            response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
            //response.addHeader("Content-Length", "" + file.length());
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            toClient.write(buffer);
            toClient.flush();
            toClient.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return response;
    }

    @PostMapping("/import")
    @ApiOperation("导入作业活动")
    @ApiImplicitParams({@ApiImplicitParam(name = "file", value = "Excel 文件", required = true, dataTypeClass = MultipartFile.class)})
    public CommonResult<BizTaskWorkImportRespVO> importExcel(@RequestParam("file") MultipartFile file) throws Exception {
        List<BizTaskWorkExcelVO> list = ExcelUtils.read(file, BizTaskWorkExcelVO.class);
        return success(taskWorkService.importExcel(list));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出作业活动 Excel")
    @PreAuthorize("@ss.hasPermission('biz:task-work:export')")
    @OperateLog(type = EXPORT)
    public void exportTaskWorkExcel(@Valid BizTaskWorkExportReqVO exportReqVO,
                                    HttpServletResponse response) throws IOException {
        List<BizTaskWorkDO> list = taskWorkService.getTaskForExcel(exportReqVO);
        // 导出 Excel
        List<BizTaskWorkExcelVO> datas = BizTaskWorkConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "作业活动.xls", "数据", BizTaskWorkExcelVO.class, datas);
    }
}
